package display 
{
	import controller.Controller;
	import display.base.ModuleBase;
	import events.DisplayEvent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import utils.DrawingShapes;
	import utils.FullScreenScrollBar;
	
	
	/**
	 * ...
	 * @author Henrik Riise Hansen
	 */
	
	public class DisplayContentList extends ModuleBase
	{
		private static var moduleName:String = 'ContentList';
		
		private var bgSprite:Sprite;
		private var font:String = 'verdana';
		private var textField:DisplayTextField;
		
		private var selectedIndex:int = 0;
		
		//sprites
		
		private var mSprite:Sprite;
		private var playerPic:BgPicture;
		private var mHolder:Sprite;
		private var buttonHolder:Sprite;
		private var thumbHolder:Sprite;
		private var tapButton:Sprite;
		private var star:Sprite;
		private var contentButtons:Sprite;
		
		// movieclips
		
		private var thumbPic:ThumbPicture;
		
		public function DisplayContentList(ctr:Controller,sh:Shell,bg:DisplayApplicationBackground) 
		{
			super(sh,moduleName, ctr,490,100,296,240,bg);
			
			build();
		}
		
		private function onFontChanged(e:DisplayEvent):void
		{
			//font = e.data.font;
			
			//createButtons();
		}
		
		// screen elements
		
		private function createButton(_colorOne:uint,_colorTwo:uint,_height:int,_width:int,_topLeft:int,_topRight:int,_bottomLeft:int,_bottomRight:int,_haveLine:Boolean):Sprite
		{
			var matr:Matrix = new Matrix();
			var sprMethod:String = SpreadMethod.PAD;
			
			matr.createGradientBox(_width,_height, Math.PI/2, 0, 0);
			
			tapButton = new Sprite;
			
			if (_haveLine)
			{
				tapButton.graphics.lineStyle(1, mainColor);
			}
			
			tapButton.graphics.beginGradientFill('linear', [_colorOne, _colorTwo], [1, 1], [0, 255], matr, sprMethod);
			tapButton.graphics.drawRoundRectComplex(0, 0, _width, _height, _topLeft, _topRight, _bottomLeft, _bottomRight);
			
			return tapButton;
		}
		
		private function createButtons():void
		{
			clear();
			
			var thumb:Sprite;
			var thumbSelected:Sprite;
			var stars:Sprite;
			var text:DisplayTextField;
			var invertedButton:Sprite;
			var playListItemExpandButton:Sprite;
			var displayHighLight:DisplayHighLight;
			var thumbGradient:DisplayHighLight;
			var thumbSelectedGradient:DisplayHighLight;
			
			thumbPic = new ThumbPicture;
			thumbPic.x = 5;
			thumbPic.y = 5;
			
			text = new DisplayTextField(font, fontColor,'Bold');
			text.x = 110;
			text.y = 5;
			text.label.text = 'Test';
			
			// selected thumb
			
			displayHighLight = new DisplayHighLight( control, 'fontColor', 108, 258);
			thumbSelectedGradient = new DisplayHighLight( control, 'thumbSelectedGradient', 108, 258);
			
			thumbSelected = createButton(thumbSelectedColorOne, thumbSelectedColorTwo, 108, 258, 2, 2, 2, 2, true);
			invertedButton = createInvertedButton();
			invertedButton.x = 220;
			invertedButton.y = 91;
			
			thumbSelected.addChild(thumbPic);
			thumbSelected.addChild(text);
			thumbSelected.addChild(invertedButton);
			thumbSelected.addChild(thumbSelectedGradient);
			thumbSelected.addChild(displayHighLight);
			
			// thumb
			
			displayHighLight = new DisplayHighLight( control, 'fontColor',67,258);
			thumbGradient = new DisplayHighLight( control, 'thumbGradient',67,258);
			
			thumbPic = new ThumbPicture;
			thumbPic.x = 5;
			thumbPic.y = 5;
			
			text = new DisplayTextField(font, fontColor,'Bold');
			text.x = 110;
			text.y = 5;
			text.label.text = 'Test';
			
			thumb = createButton(thumbColorOne, thumbColorTwo, 67, 258, 2, 2, 2, 2, false);
			thumb.y = 113;
			
			playListItemExpandButton = createPlayListItemButton();
			playListItemExpandButton.x = 220;
			playListItemExpandButton.y = 49;
			
			thumb.addChild(thumbPic);
			thumb.addChild(text);
			thumb.addChild(playListItemExpandButton);
			thumb.addChild(thumbGradient);
			thumb.addChild(displayHighLight);
			
			// holder
			
			thumbHolder.x = 495;
			thumbHolder.y = 105;
			
			thumbHolder.addChild(thumbSelected);
			thumbHolder.addChild(thumb);
			
			mSprite.addChild(thumbHolder);
		}
		
		private function createPlayListItemButton():Sprite
		{
			var playListItemButton:Sprite;
			var arrow:Sprite;
			var highlight:DisplayHighLight;
			var highlightgradient:DisplayHighLight;
			
			arrow = new Sprite;
			arrow.graphics.lineStyle(2, playListItemIconColor);
			arrow.graphics.moveTo(0, 0);
			arrow.graphics.lineTo(3, 3);
			arrow.graphics.lineTo(6, 0);
			
			arrow.x = 9;
			arrow.y = 9;
			
			highlight = new DisplayHighLight(control, 'playlistItemExpandButtonIconColor', 17, 22);
			highlightgradient = new DisplayHighLight(control, 'playlistItemExpandButtonGradient', 17, 22);
			playListItemButton = createButton(playListItemColorOne, playListItemColorTwo, 17, 22, 0, 0, 0, 0, true);
			
			playListItemButton.addChild(arrow);
			playListItemButton.addChild(highlight);
			playListItemButton.addChild(highlightgradient);
			
			return playListItemButton;
		}
		
		private function createInvertedButton():Sprite
		{
			var invertedButton:Sprite;
			var arrow:Sprite;
			var highlight:DisplayHighLight;
			var highlightgradient:DisplayHighLight;
			
			arrow = new Sprite;
			arrow.graphics.lineStyle(2, invertedButtonFontColor);
			arrow.graphics.moveTo(0, 0);
			arrow.graphics.lineTo(3, -3);
			arrow.graphics.lineTo(6, 0);
			
			arrow.x = 9;
			arrow.y = 11;
			
			highlight = new DisplayHighLight(control, 'invertedButtonFontColor', 17, 22);
			highlightgradient = new DisplayHighLight(control, 'invertedButtonGradient', 17, 22);
			invertedButton = createButton(invertedButtonColorOne, invertedButtonColorTwo, 17, 22, 0, 0, 0, 0, true);
			
			invertedButton.addChild(arrow);
			invertedButton.addChild(highlight);
		    invertedButton.addChild(highlightgradient);
			
			return invertedButton;
		}
		
		private function createStars():Sprite
		{
			var sHolder:Sprite = new Sprite;
			var s:Shape;
			
			for (var i:int; i < 5; i++)
			{
				s = new Shape;
				
				s.graphics.beginFill(ratingStarColor, 1);
				s.graphics.lineStyle(1, 0x000000);
				DrawingShapes.drawStar( s.graphics, 0, 0, 5, 3, 6, 16);
				s.graphics.endFill();
				
				s.x = i * 16;
				
				sHolder.addChild(s);
			}
				
			return sHolder;
		}
		
		override public function update():void 
		{
			super.update();
			
			createButtons();
		}
		
		private function clearBg():void
		{
			while (mSprite.numChildren != 0)
			{
				mSprite.removeChildAt(0);
			}
		}
		
		private function build():void
		{
			mSprite = new Sprite;
			thumbHolder = new Sprite;
			
			mSprite.addChild(thumbHolder);
			
			addChild(mSprite);
		}
	}
}